package com.mgq.juc.c1;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;

@Slf4j
public class TestInterrupt {

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while (true) {
                if (Thread.currentThread().isInterrupted()) {
                    log.debug("我被正常打断了....退出");
                    break;
                }
            }
        },"t1");
        Thread t2 = new Thread(() -> {

            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
                log.debug("interrupt:{}",Thread.currentThread().isInterrupted());

            }
        },"t2");
        //t1.start();
        //打断t1线程,正在运行的线程被打断时,线程不会退出,而是会生成一个打断标记,正在运行的线程可以根据这个打断标记来自行决定是否退出
        //注意,如果是sleep,wait等方法,被打断时,返回的打断标记是false
        //log.debug("打断t1");
        //  t1.interrupt();
        t2.start();
        TimeUnit.SECONDS.sleep(1);
        t2.interrupt();

    }

}
